package com.hp.activiti.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.hp.activiti.bean.Result;
import com.hp.activiti.model.Department;
import com.hp.activiti.repository.DepartmentRepository;

/**
 * @Description:部门服务类
 * @author hp
 * @date 2018年5月20日 上午9:15:59
 */
@Service
public class DepartmentService{

	@Autowired
	private DepartmentRepository departmentRep;
	
	@Cacheable("departmentCache")
	public List<Department> findDepartmentTree(String code) {
		List<Department> depData = new ArrayList<Department>();
		Department dp = departmentRep.findByDepartmentCode(code);
		List<Department> depList = departmentRep.findAll();
		
		List<Department> list = findAllChild(code, depList, new ArrayList<Department>());
		depData.add(findChildren(dp, list));
		return depData;
		
	}
	
	
	private List<Department> findAllChild(String code,List<Department> depList,List<Department> childList){
		
		if(CollectionUtils.isEmpty(childList)){
			childList = new ArrayList<Department>();
		}
		
		for(Department dp : depList){
			if(code.equals(dp.getParentsId())){             //获取下级部门信息
				childList.add(dp);
				findAllChild(dp.getDepartmentCode(),depList,childList);
			}
		}
		return childList;
	}
	
	
	private Department findChildren(Department dp, List<Department> dpList){
		dpList.forEach(action->{
			if(String.valueOf(dp.getDepartmentCode()).equals(action.getParentsId())){
				if(dp.getDepList()==null){
					dp.setDepList(new ArrayList<Department>());
				}
				dp.getDepList().add(findChildren(action, dpList));
			}
		});
		return dp;
	}


	public Result saveDepartment(Department dp) {
		try {
			departmentRep.save(dp);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
		return Result.success();
	}


	public List<Department> findDepartmentByCode(String code) {
		List<Department> depList = departmentRep.findAll();
		Department dp = departmentRep.findByDepartmentCode(code);
		List<Department> list = findAllChild(code, depList, new ArrayList<Department>());
		list.add(dp);
		return list;
	}

	/**
	 * 获取上级所有部门
	 * @param code
	 * @return
	 */
	public List<Department> findParentsBydepCode(String code) {
		List<Department> depList = departmentRep.findAll();
		List<Department> newData = findParentsDep(code, depList, null);
		Collections.reverse(newData);
		return newData;
	}
	
	private List<Department> findParentsDep(String code,List<Department> depList,List<Department> newData){
		if(CollectionUtils.isEmpty(newData)){
			newData = new ArrayList<Department>();
		}
		for(Department action : depList){
			if(code.equals(action.getDepartmentCode())){
				newData.add(action);
				if(!StringUtils.isEmpty(action.getParentsId())){
					findParentsDep(action.getParentsId(),depList,newData);
				}
			}
		}
		return newData;
	}
	
	
} 
